package com.self.reflect;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

/**
 * 作者：tianjiayuan
 * 创建时间：2018-06-13 13:30
 * 类描述：
 * 修改人：
 * 修改时间：
 */
public class ClientInvoker<T> {

    public T invoke(String url) {
        System.out.println("url:" + url);
        String[] tt = url.split("&");
        String className = tt[0];
        String methodName = tt[1];
        String[] args = null;
        if (tt.length > 2) {
            args = tt[2].split(",");
        }
        try {
            System.out.println("className:" + className);
            Class<Subject> c = (Class<Subject>) Class.forName(className);
            Subject subject = c.newInstance();
            InvocationHandler myProxy = new MyProxy(subject);
            Subject proxy = (Subject)Proxy.newProxyInstance(Subject.class.getClassLoader(), subject.getClass().getInterfaces(), myProxy);
            if ("rent".equals(methodName.substring(0,4))) {
                return (T)proxy.rent();
            }else {
                return (T)proxy.hello(args[2]);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
